<?php
namespace app\index\controller;


use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceLinModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeHuanModel;
use app\index\model\IncomeQiModel;
use app\index\model\LabourJieModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenModel;
use app\index\model\MenuModel;
use app\index\model\PaymentBaoListModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayListModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\SpeedMonthModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\StuffJieModel;
use app\index\model\StuffRuModel;
use app\index\model\SubbaoJieModel;
use app\index\model\SubbaoShenModel;
use app\index\model\VisaModel;
use app\index\service\BudgetService;
use app\index\service\EnginService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Tong extends Common
{

    public function tong_jing_zi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.business','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_jing_zi', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['id'], $item['state']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_piaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])
                    ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

                $item['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->where('budid','in',$budshou)->sum('money');

                $item['lei_piaoshou'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->sum('money');

                $item['lei_payment_engin'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_payment_bao'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('shenfee');

                $item['cha'] = $item['lei_huikuan'] - $item['lei_payment_engin'] - $item['lei_payment_bao'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_jing_lian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.business','=',2];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $gckshou = (new BudgetService())->get_budshou_sons($gckid);

        $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
        $dzkshou = (new BudgetService())->get_budshou_sons($dzkid);

        $budshou = array_merge($gckshou, $dzkshou);



        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_jing_lian', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['id'], $item['state']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $neiid = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                    ->where(['status'=>1,'isturn'=>1])->value('neiid');
                $item['dutyer'] = ContractNeiModel::where(['id'=>$neiid])->value('dutyer');

                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_piaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])
                    ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

                $item['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->where('budid','in',$budshou)->sum('money');

                $item['lei_piaoshou'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->sum('money');

                $item['lei_payment_engin'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_payment_bao'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('shenfee');

                $item['lei_payment_neikou'] = PaymentNeikouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->sum('money');

                $item['lei_payment_neifu'] = PaymentNeifuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['balance'] = $item['lei_huikuan'] - $item['lei_payment_engin'] - $item['lei_payment_bao']- $item['lei_payment_neikou']- $item['lei_payment_neifu'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_jing_dong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_jing_dong', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shihemoney'] = (new EnginService())->get_contract_shi_money($item['id']);
                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_jie'] = StuffJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += LabourShenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += SubbaoShenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += DeviceJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += LabourLingModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += DeviceLinModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_payment_bao'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('shenfee');

                $item['lei_shishi'] = $item['lei_jie'] + $item['lei_payment_bao'];
                $item['lei_lirun'] = $item['lei_chan'] - $item['lei_shishi'];

                $item['lei_lirate'] = 0;
                if($item['lei_lirun'] > 0){
                    if($item['lei_chan'] != 0){
                        $item['lei_lirate'] = round($item['lei_lirun'] / $item['lei_chan'] *100 , 2);
                    }
                }


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_jing_bian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_jing_bian', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shihemoney'] = (new EnginService())->get_contract_shi_money($item['id']);

                $item['shiid'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                    ->where(['status'=>1,'isturn'=>1])->value('id');
                $item['bumoney'] = (new EnginService())->get_contract_shi_bu_money($item['id']);

                $item['lei_qian'] = VisaModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('shemoney');

                $item['lei_zhanbi'] = 0;
                if($item['shihemoney'] != 0){
                    $item['lei_zhanbi'] = round(($item['bumoney']+$item['lei_qian']) / $item['shihemoney'] *100 , 2);
                }


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function tong_zi_jing(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_zi_jing', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shihemoney'] = (new EnginService())->get_contract_shi_money($item['id']);
                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_piaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])
                    ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

                $item['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->where('budid','in',$budshou)->sum('money');

                $item['lei_caihe'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_jie'] = StuffJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += LabourShenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += SubbaoShenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += DeviceJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += LabourLingModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += DeviceLinModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_payment_engin'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_payment_bao'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('shenfee');

                $item['lei_piaoshou'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->sum('money');

                $item['jiecha'] = $item['lei_chan'] - $item['lei_jie'] - $item['lei_payment_bao'];
                $item['hecha'] = $item['shimoney'] - $item['lei_caihe'];
                $item['xiancha'] = $item['lei_huikuan'] - $item['lei_payment_engin'] - $item['lei_payment_bao'];
                $item['yingchan'] = $item['lei_chan'] - $item['lei_huikuan'];
                $item['yingkai'] = $item['lei_piaokai'] - $item['lei_huikuan'];
                $item['yinghe'] = $item['lei_caihe'] - $item['lei_payment_engin'];
                $item['yingjie'] = $item['lei_jie'] - $item['lei_payment_engin'];
                $item['yingshou'] = $item['lei_piaoshou'] - $item['lei_payment_engin'] - $item['lei_payment_bao'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_zi_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_zi_xiang', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_piaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])
                    ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

                $item['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->where('budid','in',$budshou)->sum('money');


                $item['lei_huihe'] = 0;
                if($item['lei_huikuan'] != 0 && $item['shimoney'] != 0){
                    $item['lei_huihe'] = round($item['lei_huikuan']/$item['shimoney']*100, 2);
                }

                $item['lei_huishen'] = 0;
                if($item['lei_huikuan'] != 0 && $item['lei_chan'] != 0){
                    $item['lei_huishen'] = round($item['lei_huikuan']/$item['lei_chan']*100, 2);
                }

                $item['lei_huikai'] = 0;
                if($item['lei_huikuan'] != 0 && $item['lei_piaokai'] != 0){
                    $item['lei_huikai'] = round($item['lei_huikuan']/$item['lei_piaokai']*100, 2);
                }

                $item['lei_shenwei'] = $item['lei_chan'] - $item['lei_piaokai'];
                $item['lei_weihui'] = $item['lei_chan'] - $item['lei_huikuan'];
                $item['lei_kaiwei'] = $item['lei_piaokai'] - $item['lei_huikuan'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_zi_dan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['d.deleted_user','=', 0];
        $where[] = ['d.comid','=',$this->userinfo['comid']];
        $where[] = ['d.type','=',2];
        $where[] = ['d.status','=',1];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['d.id','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);


        $whereArr = [];
        if($this->userinfo['issuper'] < 1){
            $userChanid = (new UserService())->get_user_chanid();
            $where[] = ['d.id','=', $userChanid];
        }



        $data['data'] = DeptModel::alias('d')
            ->field('d.*')
            ->where((new MenuService())->get_miss_where('tong_zi_dan', 'd'))
            ->where($where)
            ->where($whereArr)
            ->order('d.id ASC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['shimoney'] = 0;
                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'chanid'=>$item['id'],'deleted_user'=>0])
                    ->where(['status'=>1,'isturn'=>1])->select()->toArray();
                foreach ($shiinfo as $vv){
                    $item['shimoney'] += $vv['money'];
                    $item['shimoney'] += ContractBuModel::where(['shiid'=>$vv['id'],'deleted_user'=>0])
                        ->where(['status'=>1,'isturn'=>1])->sum('money');
                }

                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['chanid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_piaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['chanid'=>$item['id'],'status'=>1,'isturn'=>1])
                    ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

                $item['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['chanid'=>$item['id']])->where('budid','in',$budshou)->sum('money');


                $item['lei_huihe'] = 0;
                if($item['lei_huikuan'] != 0 && $item['shimoney'] != 0){
                    $item['lei_huihe'] = round($item['lei_huikuan']/$item['shimoney']*100, 2);
                }

                $item['lei_huishen'] = 0;
                if($item['lei_huikuan'] != 0 && $item['lei_chan'] != 0){
                    $item['lei_huishen'] = round($item['lei_huikuan']/$item['lei_chan']*100, 2);
                }

                $item['lei_huikai'] = 0;
                if($item['lei_huikuan'] != 0 && $item['lei_piaokai'] != 0){
                    $item['lei_huikai'] = round($item['lei_huikuan']/$item['lei_piaokai']*100, 2);
                }

                $item['lei_shenwei'] = $item['lei_chan'] - $item['lei_piaokai'];
                $item['lei_weihui'] = $item['lei_chan'] - $item['lei_huikuan'];
                $item['lei_kaiwei'] = $item['lei_piaokai'] - $item['lei_huikuan'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_zi_xian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_zi_xian', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['lei_dao'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->sum('money');



                $payenginId = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->column('id');
                $item['lei_zhi'] =  AccountListModel::where(['type'=>3])->where('mid','in',$payenginId)->sum('money');

                $baoId = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->column('id');
                $item['lei_zhi'] +=  AccountListModel::where(['type'=>4])->where('mid','in',$baoId)->sum('money');

                $neifuId = PaymentNeifuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->column('id');
                $item['lei_zhi'] +=  AccountListModel::where(['type'=>6])->where('mid','in',$neifuId)->sum('money');

                $item['lei_xian'] = $item['lei_dao'] - $item['lei_zhi'];
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_zi_dian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshouGong = (new BudgetService())->get_budshou_sons($gckid);


        $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
        $budshouDian = (new BudgetService())->get_budshou_sons($dzkid);

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_zi_dian', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshouDian, $budshouGong){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['lei_dian'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->where('budid','in',$budshouDian)->sum('money');

                $item['lei_dianbi'] = 0;
                if($item['lei_dian'] != 0 && $item['shimoney'] != 0){
                    $item['lei_dianbi'] = round($item['lei_dian'] / $item['shimoney'] *100 , 2);
                }


                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_chanjin'] = 0;
                if($item['lei_chan'] != 0 && $item['shimoney'] != 0){
                    $item['lei_chanjin'] = round($item['lei_chan'] / $item['shimoney'] *100 , 2);
                }

                $item['lei_hui'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->where('budid','in',$budshouGong)->sum('money');

                $item['lei_shenying'] = $item['lei_chan'] - $item['lei_hui'];

                $item['lei_huibi'] = 0;
                if($item['lei_hui'] != 0 && $item['lei_chan'] != 0){
                    $item['lei_huibi'] = round($item['lei_hui'] / $item['lei_chan'] *100 , 2);
                }

                $item['lei_caihe'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_jie'] = StuffJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += LabourShenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += SubbaoShenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += DeviceJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += LabourLingModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += DeviceLinModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_payment_engin'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');


                $item['lei_yingfu'] = $item['lei_jie'] - $item['lei_payment_engin'];

                $item['lei_fubi'] = 0;
                if($item['lei_payment_engin'] != 0 && $item['lei_jie'] != 0){
                    $item['lei_fubi'] = round($item['lei_payment_engin'] / $item['lei_jie'] *100 , 2);
                }

                $item['lei_dianfeng'] = $item['lei_dian'] + $item['lei_yingfu'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_zi_zhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $zbjid = (new BudgetService())->get_budshou_pid_by_name('项目收入','质保金');
        $budshou = (new BudgetService())->get_budshou_sons($zbjid);


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_zi_zhi', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                    ->where(['status'=>1,'isturn'=>1])->find();
                if($shiinfo){
                    $item['zhifee'] = $shiinfo['zhifee'];
                    $item['zhinum'] = $shiinfo['zhinum'];
                    $item['zhidt'] = $shiinfo['zhidt'];
                }else{
                    $item['zhifee'] = 0;
                    $item['zhinum'] = '';
                    $item['zhidt'] = '';
                }

                $item['lei_zhaohui'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->where('budid','in',$budshou)->sum('money');

                $item['lei_weihui'] = $item['zhifee'] - $item['lei_zhaohui'];

                $item['lei_huibi'] = 0;
                if($item['zhifee'] != 0 && $item['lei_zhaohui'] != 0){
                    $item['lei_huibi'] = round($item['zhifee'] / $item['lei_zhaohui'] *100 , 2);
                }

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_zi_bao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $zbjid = (new BudgetService())->get_budshou_pid_by_name('项目收入','质保金');
        $budshou = (new BudgetService())->get_budshou_sons($zbjid);


        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.business,e.chanid,e.state')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_zi_bao', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                return $item;
            })->toArray();

        $list = [];
        foreach ($data['data']['data'] as $item){
            $bondtype = [
                ['type' => 2, 'name'=>'履约保证金'],
                ['type' => 3, 'name'=>'农民工工资保证金'],
            ];
            foreach ($bondtype as &$v){
                $item['bondtype'] = $v['type'];
                $item['bondtypename'] = $v['name'];

                $item['shou'] = EnginBondShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'bondtype'=>$v['type']])->sum('money');
                $shouId = EnginBondShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'bondtype'=>$v['type']])->column('id');
                $item['fan'] = EnginBondFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->where('shouid','in',$shouId)->sum('money');

                $item['jiao'] = EnginBondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'bondtype'=>$v['type'],'isturn'=>1,'status'=>1])->sum('money');
                $jiaoId = EnginBondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'bondtype'=>$v['type'],'isturn'=>1,'status'=>1])->column('id');
                $item['tui'] =  AccountListModel::where(['type'=>8])->where('mid','in',$jiaoId)->sum('money');

                $list[] = $item;
            }
        }

        $data['data']['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function tong_cheng_cai(Request $request){
        $param = $request->post();

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['bud.deleted_user','=',0];
        $where[] = ['bud.isturn','=',1];
        $where[] = ['bud.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $list = [];

        $info = EnginBudgetListModel::alias('list')
            ->field('list.*,e.id as enginid,e.name as enginname,e.business,e.chanid,e.state')
            ->join('engin_budget bud','bud.id=list.budid')
            ->join('engin e','e.id=bud.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_cheng_cai', 'e'))
            ->order('e.id DESC')->select()->toArray();
        foreach ($info as &$item){
            $item['zhan_num'] = ContractCaiListModel::alias('list')
                ->field('*')
                ->join('contract_cai cai','cai.id=list.caiid')
                ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0])
                ->where(['cai.isturn'=>1,'list.listid'=>$item['id']])
                ->where('cai.status','in',[0,1])
                ->sum('list.num');
            $item['zhan_money'] = ContractCaiListModel::alias('list')
                ->field('*')
                ->join('contract_cai cai','cai.id=list.caiid')
                ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0])
                ->where(['cai.isturn'=>1,'list.listid'=>$item['id']])
                ->where('cai.status','in',[0,1])
                ->sum('list.money');


            $item['ava_money'] = 0;
            if($item['zhan_num'] != 0){
                $item['ava_money'] = round($item['zhan_money'] / $item['zhan_num'], 2);
            }


            $item['sy_num'] = $item['num'] - $item['zhan_num'];
            $item['sy_money'] = $item['money'] - $item['zhan_money'];

            if($item['sy_num'] < 0 || $item['sy_money'] < 0){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $list[] = $item;
            }
        }



        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cheng_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bud.deleted_user','=', 0];
        $where[] = ['bud.comid','=',$this->userinfo['comid']];
        $where[] = ['bud.isturn','=',1];
        $where[] = ['bud.status','=',1];

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }


        $arr = [
            'cai' =>  ['name'=>'材料费','shiid'=>1],
            'ren' =>  ['name'=>'人工费','shiid'=>2],
            'ji' =>  ['name'=>'机械费','shiid'=>3],
            'fen' =>  ['name'=>'分包费','shiid'=>4],
            'cuo' =>  ['name'=>'措施费','shiid'=>5],
            'guan' =>  ['name'=>'项目费用','shiid'=>0],
        ];

        foreach ($arr as $k=>&$v){
            $topid = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'pid'=>0,'name'=>$v['name']])->value('id');
            $v['ids'] = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'topid'=>$topid])->column('id');
        }

        $data['data'] = EnginBudgetModel::alias('bud')
            ->field('bud.*,e.name as enginname')
            ->join('engin e','e.id=bud.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_cheng_xiang', 'bud'))
            ->order('bud.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($arr){
                $item['heji_yusuan'] = 0;
                $item['heji_shi'] = 0;
                $item['heji_chao'] = 0;


                foreach ($arr as $kk=>$vv){
                    $item[$kk.'_yusuan'] = EnginBudgetListModel::where(['budid'=>$item['id']])
                        ->where('subid','in',$vv['ids'])->sum('money');
                    if($kk == 'guan'){
                        $item[$kk.'_shi'] = PaymentBaoModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                            ->where(['enginid'=>$item['enginid']])
                            ->sum('money');
                    }else{
                        $item[$kk.'_shi'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                            ->where(['enginid'=>$item['enginid'],'budid'=>$vv['shiid']])
                            ->sum('money');
                    }
                    $item[$kk.'_chao'] = $item[$kk.'_shi'] - $item[$kk.'_yusuan'];

                    $item['heji_yusuan'] += $item[$kk.'_yusuan'];
                    $item['heji_shi'] += $item[$kk.'_shi'];
                    $item['heji_chao'] += $item[$kk.'_chao'];

                    $item[$kk.'_chao'] = $item[$kk.'_chao'] < 0 ? '' : $item[$kk.'_chao'];
                }

                $item['heji_chao'] = $item['heji_chao'] < 0 ? '' : $item['heji_chao'];
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cheng_cheng(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_cheng_cheng', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';

                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id']])->where('budid','in',$budshou)->sum('money');


                $item['cai_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>1])->sum('money');
                $cai_fuku_id =  PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>1])->column('id');
                $item['cai_fuku_shi'] =  AccountListModel::where(['type'=>3])->where('mid','in',$cai_fuku_id)->sum('money');

                $item['ren_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>2])->sum('money');
                $ren_fuku_id =  PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>2])->column('id');
                $item['ren_fuku_shi'] =  AccountListModel::where(['type'=>3])->where('mid','in',$ren_fuku_id)->sum('money');

                $item['zu_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>3])->sum('money');
                $zu_fuku_id =  PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>3])->column('id');
                $item['zu_fuku_shi'] =  AccountListModel::where(['type'=>3])->where('mid','in',$zu_fuku_id)->sum('money');

                $item['fen_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>4])->sum('money');
                $fen_fuku_id =  PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>4])->column('id');
                $item['fen_fuku_shi'] =  AccountListModel::where(['type'=>3])->where('mid','in',$fen_fuku_id)->sum('money');

                $item['cuo_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>5])->sum('money');
                $cuo_fuku_id =  PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'budid'=>5])->column('id');
                $item['cuo_fuku_shi'] =  AccountListModel::where(['type'=>3])->where('mid','in',$cuo_fuku_id)->sum('money');


                $item['lei_payment_bao'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('shenfee');
                $payment_bao_id =  PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->column('id');
                $item['lei_payment_bao_shi'] =  AccountListModel::where(['type'=>4])->where('mid','in',$payment_bao_id)->sum('money');



                $item['lei_cheng_he'] = $item['cai_fuku']+$item['ren_fuku']+$item['zu_fuku']+$item['fen_fuku']+$item['cuo_fuku']+$item['lei_payment_bao'];
                $item['lei_shi_he'] = $item['cai_fuku_shi']+$item['ren_fuku_shi']+$item['zu_fuku_shi']+$item['fen_fuku_shi']
                    +$item['cuo_fuku_shi']+$item['lei_payment_bao_shi'];


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function tong_cheng_rcj(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }

        $data['data'] = EnginModel::alias('e')->where($where)
            ->where((new MenuService())->get_miss_where('tong_cheng_rcj', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['cai_con'] = ContractCaiModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'caihetype'=>1])->where('status','=',1)->sum('money');
                $item['cai_stuffru'] = StuffRuModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['cai_stuffjie'] = StuffJieModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['cai_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'budid'=>1])->where('status','=',1)->sum('money');

                $item['ren_con'] = ContractCaiModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'caihetype'=>2])->where('status','=',1)->sum('money');
                $item['ren_labourshen'] = LabourShenModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['ren_labourjie'] = LabourJieModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['ren_labourling'] = LabourLingModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['ren_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'budid'=>2])->where('status','=',1)->sum('money');

                $item['zu_con'] = ContractCaiModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'caihetype'=>3])->where('status','=',1)->sum('money');
                $item['zu_devicejie'] = DeviceJieModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['zu_devicelin'] = DeviceLinModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['zu_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'budid'=>3])->where('status','=',1)->sum('money');

                $item['fen_con'] = ContractCaiModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'caihetype'=>4])->where('status','=',1)->sum('money');
                $item['fen_subbaoshen'] = SubbaoShenModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['fen_subbaojie'] = SubbaoJieModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id']])->where('status','=',1)->sum('money');
                $item['fen_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'budid'=>4])->where('status','=',1)->sum('money');

                $item['cuo_fuku'] = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$item['id'],'budid'=>5])->where('status','=',1)->sum('money');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cheng_suan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bud.deleted_user','=', 0];
        $where[] = ['bud.comid','=',$this->userinfo['comid']];
        $where[] = ['bud.isturn','=',1];
        $where[] = ['bud.status','=',1];


        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bud.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bud.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bianer']) && !empty($param['bianer'])){
            $where[] = ['bud.bianer','like', '%'.$param['bianer'].'%'];
        }
        if(isset($param['biandt']) && !empty($param['biandt'])){
            $where[] = ['bud..biandt','between', explode(' - ', $param['biandt'])];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['bud.money','like', '%'.$param['money'].'%'];
        }


        $data['data'] = EnginBudgetModel::alias('bud')
            ->field('bud.*,e.name as enginname')
            ->join('engin e','e.id=bud.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_cheng_suan', 'bud'))
            ->order('bud.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caimoney'] = 0;
                $list = EnginBudgetListModel::where(['budid'=>$item['id']])->column('id');
                foreach ($list as $listid){
                    $item['caimoney'] += ContractCaiListModel::alias('list')
                        ->join('contract_cai cai','cai.id=list.caiid')
                        ->where(['cai.deleted_user'=>0,'cai.isturn'=>1])
                        ->where('cai.status','in',[0,1])
                        ->where('list.listid','=',$listid)
                        ->sum('list.money');
                }

                $item['sheng'] = $item['money'] - $item['caimoney'];
                $item['chaorate'] = 0;
                if($item['caimoney'] != 0 && $item['money'] !=0){
                    $item['chaorate'] = round($item['caimoney']/$item['money']*100,2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cheng_guan(Request $request){
        $param = $request->post();

        $where[] = ['bao.deleted_user','=', 0];
        $where[] = ['bao.comid','=',$this->userinfo['comid']];
        $where[] = ['bao.isturn','=',1];
        $where[] = ['bao.status','in',[0,1]];

        $whereBg[] = ['bg.deleted_user','=', 0];
        $whereBg[] = ['bg.comid','=',$this->userinfo['comid']];
        $whereBg[] = ['bg.isturn','=',1];
        $whereBg[] = ['bg.status','=',1];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['bao.enginid','=',$param['enginid']];
            $whereBg[] = ['bg.enginid','=',$param['enginid']];
        }

        $data['enginid'] = EnginModel::alias('e')
            ->field('e.id as value,e.name')
            ->where((new MenuService())->get_miss_where('tong_cheng_guan', 'e'))
            ->where(['e.comid'=>$this->userinfo['comid'],'e.deleted_user'=>0])
            ->where(['e.isturn'=>1,'e.status'=>1])->select()->toArray();
        $data['is_enginid'] = isset($param['enginid']) ? $param['enginid'] : '';

        $year = $param['year'];
        $enginid = isset($param['enginid']) && !empty($param['enginid']) ? $param['enginid'] : 0;


        $baolist = PaymentBaoModel::alias('bao')
            ->join('engin e','e.id=bao.enginid')
            ->field('bao.id,bao.budid,bao.shenfee as zhimoney,bao.created_at')
            ->where((new MenuService())->get_miss_where('tong_cheng_guan', 'e'))
            ->where($where)->select()->toArray();

        $budgetlist = EnginBudgetListModel::alias('list')
            ->field('list.subid,list.money')
            ->join('engin_budget bg','bg.id=list.budid')
            ->join('engin e','e.id=bg.enginid')
            ->where((new MenuService())->get_miss_where('tong_cheng_guan', 'e'))
            ->where($whereBg)->select()->toArray();


        $topid = EnginBudsubModel::where(['name'=>'项目费用'])->value('id');
        $kemu = EnginBudsubModel::field('id,name,pid')->where(['comid'=>$this->userinfo['comid'],'status'=>1])
            ->where("id=".$topid." or topid=".$topid)->select()->toArray();

        foreach ($kemu as &$v){
            $v['year'] = $year;
            $v['enginid'] = $enginid;
            $v['zhimoney'] = 0;
            $v['son'] = (new BudgetService())->get_budsub_sonsss($v['id']);

            for ($i=1; $i<=12; $i++){
                $v['zhi'.$i] = 0;
            }
            $v['zhiyear'] = 0;

            foreach ($baolist as $bao){
                $creat_y = (int)date("Y", strtotime($bao['created_at']));
                $creat_m = (int)date("m", strtotime($bao['created_at']));
                if($bao['budid'] == $v['id']){
                    $v['zhimoney'] += $bao['zhimoney'];
                    for ($i=1; $i<=12; $i++){
                        if($creat_y == $year && $creat_m == $i){
                            $v['zhi'.$i] += $bao['zhimoney'];
                        }
                    }
                }else{
                    if(in_array($bao['budid'], $v['son'])){
                        $v['zhimoney'] += $bao['zhimoney'];
                        for ($i=1; $i<=12; $i++){
                            if($creat_y == $year && $creat_m == $i){
                                $v['zhi'.$i] += $bao['zhimoney'];
                            }
                        }
                    }
                }
            }


            $v['bgmoney'] = 0;
            foreach ($budgetlist as $bgt){
                if($bgt['subid'] == $v['id'] || in_array($bgt['subid'], $v['son'])) {
                    $v['bgmoney'] += $bgt['money'];
                }
            }


            for ($i=1; $i<=12; $i++){
                $v['zhiyear'] += $v['zhi'.$i];
            }
        }

        $data['data'] = array2tree($kemu, 'id', 'pid', 'children');

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cheng_guan_mingxi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bao.deleted_user','=', 0];
        $where[] = ['bao.comid','=',$this->userinfo['comid']];
        $where[] = ['bao.isturn','=',1];
        $where[] = ['bao.status','in',[0,1]];


        if(isset($param['budid']) && !empty($param['budid'])){
            $son = (new BudgetService())->get_budsub_sonsss($param['budid']);
            $where[] = ['bao.budid','in',$son];
        }

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['bao.enginid','=', $param['enginid']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            if(isset($param['month']) && !empty($param['month'])){
                $firstday = date('Y-m-01 00:00:00', strtotime($param['year'].'-'.$param['month'].'-01'));
                $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day"));
                $where[] = ['bao.created_at','between', [$firstday, $lastday]];
            }else{
                $firstday = $param['year'].'-01-01 00:00:00';
                $lastday = $param['year'].'-12-31 23:59:59';
                $where[] = ['bao.created_at','between', [$firstday, $lastday]];
            }
        }


        $data['data'] = PaymentBaoModel::alias('bao')
            ->field('bao.*,e.name as enginname')
            ->leftJoin('engin e','e.id=bao.enginid')
            ->where($where)
            ->order('bao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cheng_zeng(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('id,name,business')
            ->where($where)
            ->order('e.id DESC')
            ->where((new MenuService())->get_miss_where('tong_cheng_zeng', 'e'))
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['faer'] = '';
                $item['chenger'] = '';
                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($shiinfo){
                    $item['faer'] = $shiinfo['faer'];
                    $item['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
                }




                $item['kai_all'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.money');
                $item['kai_nofee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.fee');
                $item['kai_ratefee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.ratefee');


                $item['shou_all'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.money');
                $item['shou_jin'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->where(['list.piaotype'=>1])
                    ->sum('list.ratefee');

                $item['yingfee'] = $item['kai_ratefee'] - $item['shou_jin'];
                $item['yingfee'] = $item['yingfee'] < 0 ? 0 : $item['yingfee'];


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function tong_chan_jin(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zong.deleted_user','=', 0];
        $where[] = ['zong.comid','=',$this->userinfo['comid']];


        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['zong.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['zong.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['zong.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['zong.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['zong.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['zong.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = SpeedZongModel::alias('zong')
            ->field('zong.*,e.name as enginname')
            ->join('engin e','e.id=zong.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_chan_jin', 'zong'))
            ->order('zong.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wanchan'] = 0;
                $item['wanbi'] = 0;
                $item['findt'] = '';
                $item['jinbi'] = '';

                $zongList = SpeedZongListModel::where(['zongid'=>$item['id']])->select()->toArray();
                foreach ($zongList as $v){
                    $findt = EnginSpeedListModel::alias('list')
                        ->join('engin_speed speed','speed.id=list.speedid')
                        ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                        ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$v['id']])
                        ->value('list.findt');
                    if(!empty($findt)){
                        if(empty($item['findt'])){
                            $item['findt'] = $findt;
                        }else{
                            if($findt <= $item['findt']){
                                $item['findt'] = $findt;
                            }
                        }
                    }


                    $shinum = EnginSpeedListModel::alias('list')
                        ->join('engin_speed speed','speed.id=list.speedid')
                        ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                        ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$v['id']])
                        ->sum('list.shinum');
                    $item['wanchan'] += $shinum*$v['price'];
                }

                if($item['chanfee'] != 0){
                    $item['wanbi'] = round($item['wanchan'] / $item['chanfee'] *100, 2);
                }


                if(!empty($item['findt'])){
                    $jihua = get_time_slot_days($item['startdt'], $item['enddt']);
                    $shiji = get_time_slot_days($item['findt'], date("Y-m-d"));
                    if($jihua != 0){
                        $item['jinbi'] = round($shiji/$jihua*100, 2);
                    }
                }
                if(!$item['jinbi']){
                    $item['jinbi'] = 0;
                }


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_chan_yue(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $year = $param['year'];

        $data['data'] = EnginModel::alias('e')->field('*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_chan_yue', 'e'))
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($year){
                $item['year'] = $year;
                $zong = SpeedZongModel::where(['enginid'=>$item['id'],'deleted_user'=>0])->find();
                $item['chanfee'] = isset($zong['chanfee']) ? $zong['chanfee'] : 0;

                $item['wanchan'] = 0;
                $item['wanbi'] = 0;
                $item['leichan'] = 0;

                for ($i=1; $i<=12; $i++){
                    $item['wan'.$i] = 0;
                    $item['shen'.$i] = 0;
                }
                $item['wanyear'] = 0;
                $item['shenyear'] = 0;


                $chanList = EnginChanModel::field('month,leijin')
                    ->where(['deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id']])
                    ->select()->toArray();
                foreach ($chanList as $vv){
                    for ($i=1; $i<=12; $i++){
                        $days = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;

                        if($vv['month'] == $days){
                            $item['shen'.$i] += $vv['leijin'];
                            $item['shenyear'] += $vv['leijin'];
                        }
                    }
                    $item['leichan'] += $vv['leijin'];
                }


                $zongList = SpeedZongListModel::where(['zongid'=>$zong['id']])->select()->toArray();
                foreach ($zongList as $v){
                    $baoList = EnginSpeedListModel::alias('list')
                        ->field('speed.month,list.shinum')
                        ->join('engin_speed speed','speed.id=list.speedid')
                        ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                        ->where(['speed.enginid'=>$item['id'],'list.zongid'=>$v['id']])
                        ->select()->toArray();
                    foreach ($baoList as $vv){
                        $value = $vv['shinum'] * $v['price'];
                        for ($i=1; $i<=12; $i++){
                            $days = $i < 10 ? $year.'-0'.$i : $year.'-'.$i;
                            if($vv['month'] == $days){
                                $item['wan'.$i] += $value;
                                $item['wanyear'] += $value;
                            }
                        }
                        $item['wanchan'] += $value;
                    }



                }

                if($item['chanfee'] != 0){
                    $item['wanbi'] = round($item['wanchan'] / $item['chanfee'] *100, 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function tong_gong_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];


        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }

        $list = [];
        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.gonger')
            ->where($where)
            ->group('gonger')
            ->where((new MenuService())->get_miss_where('tong_gong_gong', 'cai'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();
        foreach ($data['data']['data'] as $item){
            $enginidArr = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['gonger'=>$item['gonger'],'isturn'=>1,'status'=>1])
                ->group('enginid')->column('enginid');

            $engininfo = EnginModel::where('id','in',$enginidArr)->where(['status'=>1,'isturn'=>1])->select()->toArray();
            $xiaoji = [
                'gonger' => $item['gonger'],
                'name' => '小计',
                'lei_caihe' => 0,
                'lei_jie' => 0,
                'lei_piaoshou' => 0,
                'lei_payment_engin' => 0,
            ];
            foreach ($engininfo as $vv){
                $vv['gonger'] = $item['gonger'];
                $vv['state_name'] = (new EnginService())->get_engin_state_name_tag($vv['id'], $vv['state']);
                $vv['business_name'] = $vv['business'] == 1 ? '自营' : '联营';
                $vv['chanid_name'] = DeptModel::get_name_by_pk($vv['chanid']);

                $vv['lei_caihe'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$vv['id'],'gonger'=>$item['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $xiaoji['lei_caihe'] += $vv['lei_caihe'];

                $vv['lei_jie'] = StuffJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$vv['id'],'gonger'=>$item['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $vv['lei_jie'] += LabourShenModel::alias('shen')
                    ->join('contract_cai cai','cai.id=shen.caiid')
                    ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
                    ->where(['shen.enginid'=>$vv['id'],'cai.gonger'=>$item['gonger'],'shen.status'=>1,'shen.isturn'=>1])
                    ->sum('shen.money');
                $vv['lei_jie'] += SubbaoShenModel::alias('shen')
                    ->join('contract_cai cai','cai.id=shen.caiid')
                    ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
                    ->where(['shen.enginid'=>$vv['id'],'cai.gonger'=>$item['gonger'],'shen.status'=>1,'shen.isturn'=>1])
                    ->sum('shen.money');
                $vv['lei_jie'] += DeviceJieModel::alias('shen')
                    ->join('contract_cai cai','cai.id=shen.caiid')
                    ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
                    ->where(['shen.enginid'=>$vv['id'],'cai.gonger'=>$item['gonger'],'shen.status'=>1,'shen.isturn'=>1])
                    ->sum('shen.money');
                $vv['lei_jie'] += LabourLingModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$vv['id'],'gonger'=>$item['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $vv['lei_jie'] += DeviceLinModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$vv['id'],'gonger'=>$item['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $xiaoji['lei_jie'] += $vv['lei_jie'];

                $vv['lei_piaoshou'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou shou','shou.id=list.shouid')
                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
                    ->where(['shou.enginid'=>$vv['id'],'list.gonger'=>$item['gonger'],'shou.status'=>1,'shou.isturn'=>1])
                    ->sum('list.money');
                $xiaoji['lei_piaoshou'] += $vv['lei_piaoshou'];

                $vv['lei_payment_engin'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$vv['id'],'gonger'=>$item['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $xiaoji['lei_payment_engin'] += $vv['lei_payment_engin'];


                $vv['lei_bijie'] = 0;
                if($vv['lei_payment_engin'] != 0 && $vv['lei_jie'] != 0){
                    $vv['lei_bijie'] = round($vv['lei_payment_engin'] / $vv['lei_jie'] *100 , 2);
                }

                $vv['lei_bihe'] = 0;
                if($vv['lei_payment_engin'] != 0 && $vv['lei_caihe'] != 0){
                    $vv['lei_bihe'] = round($vv['lei_payment_engin'] / $vv['lei_caihe'] *100 , 2);
                }

                $vv['lei_daijie'] = $vv['lei_jie'] - $vv['lei_payment_engin'];
                $vv['lei_daihe'] = $vv['lei_caihe'] - $vv['lei_payment_engin'];

                $list[] = $vv;
            }

            $xiaoji['lei_bijie'] = 0;
            if($xiaoji['lei_payment_engin'] != 0 && $xiaoji['lei_jie'] != 0){
                $xiaoji['lei_bijie'] = round($xiaoji['lei_payment_engin'] / $xiaoji['lei_jie'] *100 , 2);
            }

            $xiaoji['lei_bihe'] = 0;
            if($xiaoji['lei_payment_engin'] != 0 && $xiaoji['lei_caihe'] != 0){
                $xiaoji['lei_bihe'] = round($xiaoji['lei_payment_engin'] / $xiaoji['lei_caihe'] *100 , 2);
            }

            $xiaoji['lei_daijie'] = $xiaoji['lei_jie'] - $xiaoji['lei_payment_engin'];
            $xiaoji['lei_daihe'] = $xiaoji['lei_caihe'] - $xiaoji['lei_payment_engin'];
            if(count($engininfo) > 1){
                $list[] = $xiaoji;
            }
        }



        $data['data']['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_gong_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=',1];
        $where[] = ['e.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $list = [];
        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.business,e.chanid,e.state')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_gong_xiang', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                return $item;
            })->toArray();
        foreach ($data['data']['data'] as $item){
            $gongArr = ContractCaiModel::field('gonger')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['enginid'=>$item['id'],'isturn'=>1,'status'=>1])
                ->group('gonger')->select()->toArray();

            $xiaoji = [
                'name' => $item['name'],
                'gonger' => '小计',
                'lei_caihe' => 0,
                'lei_jie' => 0,
                'lei_piaoshou' => 0,
                'lei_payment_engin' => 0,
            ];
            foreach ($gongArr as $vv){
                $item['gonger'] = $vv['gonger'];

                $item['lei_caihe'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'gonger'=>$vv['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $xiaoji['lei_caihe'] += $item['lei_caihe'];

                $item['lei_jie'] = StuffJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'gonger'=>$vv['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += LabourShenModel::alias('shen')
                    ->join('contract_cai cai','cai.id=shen.caiid')
                    ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
                    ->where(['shen.enginid'=>$item['id'],'cai.gonger'=>$vv['gonger'],'shen.status'=>1,'shen.isturn'=>1])
                    ->sum('shen.money');
                $item['lei_jie'] += SubbaoShenModel::alias('shen')
                    ->join('contract_cai cai','cai.id=shen.caiid')
                    ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
                    ->where(['shen.enginid'=>$item['id'],'cai.gonger'=>$vv['gonger'],'shen.status'=>1,'shen.isturn'=>1])
                    ->sum('shen.money');
                $item['lei_jie'] += DeviceJieModel::alias('shen')
                    ->join('contract_cai cai','cai.id=shen.caiid')
                    ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
                    ->where(['shen.enginid'=>$item['id'],'cai.gonger'=>$vv['gonger'],'shen.status'=>1,'shen.isturn'=>1])
                    ->sum('shen.money');
                $item['lei_jie'] += LabourLingModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'gonger'=>$vv['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += DeviceLinModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'gonger'=>$vv['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $xiaoji['lei_jie'] += $item['lei_jie'];

                $item['lei_piaoshou'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou shou','shou.id=list.shouid')
                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
                    ->where(['shou.enginid'=>$item['id'],'list.gonger'=>$vv['gonger'],'shou.status'=>1,'shou.isturn'=>1])
                    ->sum('list.money');
                $xiaoji['lei_piaoshou'] += $item['lei_piaoshou'];

                $item['lei_payment_engin'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'gonger'=>$vv['gonger'],'status'=>1,'isturn'=>1])->sum('money');
                $xiaoji['lei_payment_engin'] += $item['lei_payment_engin'];


                $item['lei_bijie'] = 0;
                if($item['lei_payment_engin'] != 0 && $item['lei_jie'] != 0){
                    $item['lei_bijie'] = round($item['lei_payment_engin'] / $item['lei_jie'] *100 , 2);
                }

                $item['lei_bihe'] = 0;
                if($item['lei_payment_engin'] != 0 && $item['lei_caihe'] != 0){
                    $item['lei_bihe'] = round($item['lei_payment_engin'] / $item['lei_caihe'] *100 , 2);
                }

                $item['lei_daijie'] = $item['lei_jie'] - $item['lei_payment_engin'];
                $item['lei_daihe'] = $item['lei_caihe'] - $item['lei_payment_engin'];

                $list[] = $item;
            }

            $xiaoji['lei_bijie'] = 0;
            if($xiaoji['lei_payment_engin'] != 0 && $xiaoji['lei_jie'] != 0){
                $xiaoji['lei_bijie'] = round($xiaoji['lei_payment_engin'] / $xiaoji['lei_jie'] *100 , 2);
            }

            $xiaoji['lei_bihe'] = 0;
            if($xiaoji['lei_payment_engin'] != 0 && $xiaoji['lei_caihe'] != 0){
                $xiaoji['lei_bihe'] = round($xiaoji['lei_payment_engin'] / $xiaoji['lei_caihe'] *100 , 2);
            }

            $xiaoji['lei_daijie'] = $xiaoji['lei_jie'] - $xiaoji['lei_payment_engin'];
            $xiaoji['lei_daihe'] = $xiaoji['lei_caihe'] - $xiaoji['lei_payment_engin'];
            if(count($gongArr) == 0){
                $list[] = $item;
            }elseif(count($gongArr) == 1){

            }else{
                $list[] = $xiaoji;
            }
        }



        $data['data']['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function tong_cai_shou(Request $request){
        $param = $request->post();

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.deleted_user','=',0];
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $year = $request->param('year', date('Y'),'int');
        $data['year'] = $year;
        $where[] = ['list.dt','between', [$year.'-01-01', $year.'-12-31']];
        $where[] = ['list.type','=', 1];

        $accountlist = AccountListModel::alias('list')
            ->field("budid,money,date_format(dt,'%m') as mon")
            ->join('account acc','acc.id=list.accid')
            ->where($where)->select()->toArray();

        $topid = EnginBudshouModel::where(['name'=>'项目收入','pid'=>0])->value('id');
        $kemu = EnginBudshouModel::field('id,name,pid')->where(['comid'=>$this->userinfo['comid'],'status'=>1])
            ->where("id=".$topid." or topid=".$topid)->select()->toArray();

        foreach ($kemu as &$v){
            $v['chanid'] = $data['is_chanid'] ? $data['is_chanid'] : 0;
            $v['year'] = $year;
            $v['count'] = 0;
//            $v['son'] = EnginBudshouModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'pid'=>$v['id']])->column('id');
            $v['son'] = (new BudgetService())->get_budshou_sons($v['id']);


            for ($i=1; $i<=12; $i++){
                $v['mon'.$i] = 0;

                foreach ($accountlist as $bao){
                    if($bao['budid'] == $v['id']){
                        if((int)$bao['mon'] == $i){
                            $v['mon'.$i] += $bao['money'];
                        }
                    }else{
                        if(in_array($bao['budid'], $v['son'])){
                            if((int)$bao['mon'] == $i){
                                $v['mon'.$i] += $bao['money'];
                            }
                        }
                    }
                }

                $v['count'] += $v['mon'.$i];
            }
        }

        $data['data'] = array2tree($kemu, 'id', 'pid', 'children');

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cai_shoufei(Request $request){
        $param = $request->post();

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.deleted_user','=',0];
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $year = $request->param('year', date('Y'),'int');
        $data['year'] = $year;
        $where[] = ['list.dt','between', [$year.'-01-01', $year.'-12-31']];
        $where[] = ['list.type','in', [2,8,9,11,14]];

        $accountlist = AccountListModel::alias('list')
            ->field("budid,money,date_format(dt,'%m') as mon")
            ->join('account acc','acc.id=list.accid')
            ->where($where)->select()->toArray();


        $topid = EnginBudshouModel::where(['name'=>'其他收入','pid'=>0])->value('id');
        $kemu = EnginBudshouModel::field('id,name,pid')->where(['comid'=>$this->userinfo['comid'],'status'=>1])
            ->where("id=".$topid." or topid=".$topid)->select()->toArray();
        foreach ($kemu as &$v){
            $v['chanid'] = $data['is_chanid'] ? $data['is_chanid'] : 0;
            $v['year'] = $year;
            $v['count'] = 0;
//            $v['son'] = EnginBudshouModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'pid'=>$v['id']])->column('id');
            $v['son'] = (new BudgetService())->get_budshou_sons($v['id']);

            for ($i=1; $i<=12; $i++){
                $v['mon'.$i] = 0;

                foreach ($accountlist as $bao){
                    if($bao['budid'] == $v['id']){
                        if((int)$bao['mon'] == $i){
                            $v['mon'.$i] += $bao['money'];
                        }
                    }else{
                        if(in_array($bao['budid'], $v['son'])){
                            if((int)$bao['mon'] == $i){
                                $v['mon'.$i] += $bao['money'];
                            }
                        }
                    }
                }

                $v['count'] += $v['mon'.$i];
            }
        }

        $data['data'] = array2tree($kemu, 'id', 'pid', 'children');

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cai_zhi(Request $request){
        $param = $request->post();

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.deleted_user','=',0];
        $where[] = ['list.type','not in', [1,2,8,9,11,14]];
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $year = $request->param('year', date('Y'),'int');
        $data['year'] = $year;
        $where[] = ['list.dt','between', [$year.'-01-01', $year.'-12-31']];


        $list = [
            ['type'=>1,'name'=>'材料费', 'zhimoney'=>0],
            ['type'=>2,'name'=>'人工费', 'zhimoney'=>0],
            ['type'=>3,'name'=>'机械费', 'zhimoney'=>0],
            ['type'=>4,'name'=>'分包费', 'zhimoney'=>0],
            ['type'=>5,'name'=>'措施费', 'zhimoney'=>0],
            ['type'=>66,'name'=>'项目费用', 'zhimoney'=>0],
        ];

        foreach ($list as &$item){
            $item['chanid'] = $data['is_chanid'] ? $data['is_chanid'] : 0;
            $item['year'] = $year;
            $item['count'] = 0;

            if($item['type'] == 66){
                $topid = EnginBudsubModel::where(['name'=>$item['name'],'pid'=>0])->value('id');
                $sonid = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'status'=>1])
                    ->where("id=".$topid." or topid=".$topid)->column('id');
                $accountlist = AccountListModel::alias('list')
                    ->field("budid,money,date_format(dt,'%m') as mon")
                    ->join('account acc','acc.id=list.accid')
                    ->where($where)->where(['type'=>4])->select()->toArray();
            }else{
                $accountlist = AccountListModel::alias('list')
                    ->field("budid,money,date_format(dt,'%m') as mon")
                    ->join('account acc','acc.id=list.accid')
                    ->where($where)->where(['type'=>3,'budid'=>$item['type']])->select()->toArray();
            }

            for ($i=1; $i<=12; $i++){
                $item['mon'.$i] = 0;

                foreach ($accountlist as $bao){
                    if($item['type'] == 66){
                        if(in_array($bao['budid'], $sonid)){
                            if((int)$bao['mon'] == $i){
                                $item['mon'.$i] += $bao['money'];
                            }
                        }
                    }else{
                        if($item['type'] == $bao['budid']){
                            if((int)$bao['mon'] == $i){
                                $item['mon'.$i] += $bao['money'];
                            }
                        }
                    }
                }

                $item['count'] += $item['mon'.$i];
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cai_zhifei(Request $request){
        $param = $request->post();

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.deleted_user','=',0];
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $year = $request->param('year', date('Y'),'int');
        $data['year'] = $year;
        $where[] = ['list.dt','between', [$year.'-01-01', $year.'-12-31']];
        $where[] = ['list.type','in', [5,7,10,12,13,15]];

        $accountlist = AccountListModel::alias('list')
            ->field("budid,money,date_format(dt,'%m') as mon")
            ->join('account acc','acc.id=list.accid')
            ->where($where)->select()->toArray();


        $topid = EnginBudsubModel::where(['name'=>'公司费用','pid'=>0])->value('id');
        $kemu = EnginBudsubModel::field('id,name,pid')->where(['comid'=>$this->userinfo['comid'],'status'=>1])
            ->where("id=".$topid." or topid=".$topid)->select()->toArray();
        foreach ($kemu as &$v){
            $v['chanid'] = $data['is_chanid'] ? $data['is_chanid'] : 0;
            $v['year'] = $year;
            $v['count'] = 0;
//            $v['son'] = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'pid'=>$v['id']])->column('id');
            $v['son'] = (new BudgetService())->get_budsub_sonsss($v['id']);


            for ($i=1; $i<=12; $i++){
                $v['mon'.$i] = 0;

                foreach ($accountlist as $bao){
                    if($bao['budid'] == $v['id']){
                        if((int)$bao['mon'] == $i){
                            $v['mon'.$i] += $bao['money'];
                        }
                    }else{
                        if(in_array($bao['budid'], $v['son'])){
                            if((int)$bao['mon'] == $i){
                                $v['mon'.$i] += $bao['money'];
                            }
                        }
                    }
                }

                $v['count'] += $v['mon'.$i];
            }
        }

        $data['data'] = array2tree($kemu, 'id', 'pid', 'children');

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function tong_cai_xian(Request $request){
        $param = $request->post();

        $where[] = ['comid','=',$this->userinfo['comid']];

        $year = $request->param('year', date("Y"), 'int');
        if(isset($param['accid']) && !empty($param['accid'])){
            $where[] = ['accid','=',$param['accid']];
        }

        $num = 12;
        if($year == date("Y")){
            $num = date("m");
        }


        $list = [];
        for ($i=1; $i<=$num; $i++){
            $month = $i < 10 ? '0'.$i : $i;

            $start = $year.'-'.$month.'-01';
            $end = date('Y-m-d', strtotime($start." +1 month -1 day"));
            $daofee = AccountListModel::where($where)
                ->where('type','in',[1,2,8,9,11,14])
                ->where('dt','between',[$start,$end])
                ->sum('money');
            $zhifee = AccountListModel::where($where)
                ->where('type','not in',[1,2,8,9,11,14])
                ->where('dt','between',[$start,$end])
                ->sum('money');


            $list[] = [
                'name' => $year.'-'.$month,
                'daofee' => $daofee,
                'zhifee' => $zhifee,
                'cha' => $daofee - $zhifee,
            ];
        }



        $accidarr = AccountModel::field('*,id as value')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();
        foreach ($accidarr as &$item){
            $item['name'] = $item['name'].$item['accnum'];
        }
        $data['accid'] = $accidarr;
        $data['is_accid'] = isset($param['accid']) ? $param['accid'] : '';


        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cai_account(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['acc.deleted_user','=', 0];
        $where[] = ['acc.comid','=',$this->userinfo['comid']];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['acc.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['accnum']) && !empty($param['accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }
        if(isset($param['accmoney']) && !empty($param['accmoney'])){
            $where[] = ['acc.accmoney','like', '%'.$param['accmoney'].'%'];
        }
        if(isset($param['accdt']) && !empty($param['accdt'])){
            $where[] = ['acc.accdt','between', explode(' - ', $param['accdt'])];
        }



        $whereMonth = [];
        if(isset($param['month']) && !empty($param['month'])){
            $seamonth = explode(' - ', $param['month']);
            $firstday = $seamonth[0].'-01';
            $ssfist = $seamonth[1].'-01';
            $lastday = date('Y-m-d', strtotime("$ssfist +1 month -1 day"));
            $whereMonth[] = ['dt','between', [$firstday, $lastday]];
        }
        $is_month = isset($param['month']) ? $param['month'] : '';
        $data['is_month'] = $is_month;

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';




        $data['data'] = AccountModel::alias('acc')
            ->field('acc.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tong_cai_account', 'acc'))
            ->order('acc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($whereMonth, $is_month){
                $item['seamonth'] = $is_month;
                $item['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);
                $item['chanid_name'] = DeptModel::where(['id'=>$item['chanid']])->value('name');


                $item['shou'] = AccountListModel::where(['accid'=>$item['id']])->where('type','in',[1,2,8,9,11,14])->where($whereMonth)->sum('money');
                $item['zhi'] = AccountListModel::where(['accid'=>$item['id']])->where('type','not in',[1,2,8,9,11,14])->where($whereMonth)->sum('money');
                $item['balance'] = round($item['shou'] - $item['zhi'], 2);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cai_piao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['status','=',1];
        $where[] = ['isturn','=',1];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = EnginModel::field('id,name,business')
            ->where($where)
            ->order('id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['faer'] = '';
                $item['chenger'] = '';
                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($shiinfo){
                    $item['faer'] = $shiinfo['faer'];
                    $item['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
                }




                $item['kai_all'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.money');
                $item['kai_nofee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.fee');
                $item['kai_ratefee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.ratefee');


                $item['shou_all'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.money');
                $item['shou_jin'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->where(['list.piaotype'=>1])
                    ->sum('list.ratefee');

                $item['yingfee'] = $item['kai_ratefee'] - $item['shou_jin'];
                $item['yingfee'] = $item['yingfee'] < 0 ? 0 : $item['yingfee'];


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function tong_cai_paybao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bao.deleted_user','=', 0];
        $where[] = ['bao.comid','=',$this->userinfo['comid']];
        $where[] = ['bao.status','=',1];
        $where[] = ['bao.isturn','=',1];


        //    发生日期  申请人  申请时间
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bao.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['bao.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['budid','=', $param['budid']];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['baocon']) && !empty($param['baocon'])){
            $where[] = ['list.baocon','like', '%'.$param['baocon'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['bao.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $where[] = ['list.created_at','between', explode(' - ', $param['created_at'])];
        }

        $data['budid'] = (new BudgetService())->get_engin_budsub_guan_all_xiala_sea();
        $data['is_budid'] = isset($param['budid']) ? $param['budid'] : '';


        $data['data'] = PaymentBaoListModel::alias('list')
            ->field('bao.*,e.name as enginname,list.name as listname,list.money as listmoney,list.baocon as listbaocon
            ,list.dt as listdt,list.explain as listexplain')
            ->join('payment_bao bao','bao.id=list.baoid')
            ->join('engin e','e.id=bao.enginid')
            ->where($where)
            ->order('bao.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tong_cai_paygong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['gong.deleted_user','=', 0];
        $where[] = ['gong.comid','=',$this->userinfo['comid']];
        $where[] = ['gong.status','=',1];
        $where[] = ['gong.isturn','=',1];

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['gong.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['gong.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['budid','=', $param['budid']];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['baocon']) && !empty($param['baocon'])){
            $where[] = ['list.baocon','like', '%'.$param['baocon'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['gong.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $where[] = ['list.created_at','between', explode(' - ', $param['created_at'])];
        }

        $data['budid'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala_sea();
        $data['is_budid'] = isset($param['budid']) ? $param['budid'] : '';


        $data['data'] = PaymentGongListModel::alias('list')
            ->field('gong.*,list.name as listname,list.money as listmoney,list.baocon as listbaocon
            ,list.dt as listdt,list.explain as listexplain')
            ->join('payment_gong gong','gong.id=list.gongid')
            ->where($where)
            ->order('gong.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_subid_quanname($item['budid']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }









    public function tong_pub_gonglei(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['self.deleted_user','=', 0];
        $where[] = ['self.comid','=',$this->userinfo['comid']];
        $where[] = ['self.isturn','=',1];
        $where[] = ['self.status','=',1];
        $where[] = ['self.enginid','=',$param['enginid']];

        $where222 = [];
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where222[] = ['self.caiid','=',$param['caiid']];
        }

        $list = [];

        if(!isset($param['lai']) || empty($param['lai']) || $param['lai'] == 'stuff_jie'){
            $whereGonger = '';
            if(isset($param['gonger']) && !empty($param['gonger'])){
                $whereGonger = "(self.caiid > 0 and cai.gonger like '%".$param['gonger']."%')";
                $whereGonger .= " or (self.caiid < 1 and self.gonger like '%".$param['gonger']."%')";
            }
            $whereCai = [];
            if(isset($param['cainame']) && !empty($param['cainame'])){
                $whereCai[] = ['cai.name','like', '%'.$param['cainame'].'%'];
            }
            $info = StuffJieModel::alias('self')
                ->field('self.*,e.name as enginname,cai.name as caiid_name,cai.gonger as caigonger')
                ->join('engin e','e.id=self.enginid')
                ->leftJoin('contract_cai cai','cai.id=self.caiid')
                ->where($where)->where($where222)->where($whereGonger)->where($whereCai)->select()->toArray();
            foreach ($info as $item){
                $item['lai'] = '材料结算';
                $item['table'] = 'stuff_jie';
                if($item['caiid'] > 0){
                    $item['gonger'] = $item['caigonger'];
                }
                $item['dt'] = $item['jiedt'];
                $list[] = $item;
            }
        }

        if(!isset($param['lai']) || empty($param['lai']) || $param['lai'] == 'labour_shen'){
            $whereGonger = [];
            if(isset($param['gonger']) && !empty($param['gonger'])){
                $whereGonger[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
            }
            $whereCai = [];
            if(isset($param['cainame']) && !empty($param['cainame'])){
                $whereCai[] = ['cai.name','like', '%'.$param['cainame'].'%'];
            }
            $info = LabourShenModel::alias('self')
                ->field('self.*,e.name as enginname,cai.name as caiid_name,cai.gonger')
                ->join('engin e','e.id=self.enginid')
                ->leftJoin('contract_cai cai','cai.id=self.caiid')
                ->where($where)->where($where222)->where($whereGonger)->where($whereCai)->select()->toArray();
            foreach ($info as $item){
                $item['lai'] = '劳务进度款';
                $item['table'] = 'labour_shen';
                $list[] = $item;
            }
        }

        if(!isset($param['lai']) || empty($param['lai']) || $param['lai'] == 'subbao_shen'){
            $whereGonger = [];
            if(isset($param['gonger']) && !empty($param['gonger'])){
                $whereGonger[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
            }
            $whereCai = [];
            if(isset($param['cainame']) && !empty($param['cainame'])){
                $whereCai[] = ['cai.name','like', '%'.$param['cainame'].'%'];
            }
            $info = SubbaoShenModel::alias('self')
                ->field('self.*,e.name as enginname,cai.name as caiid_name,cai.gonger')
                ->join('engin e','e.id=self.enginid')
                ->leftJoin('contract_cai cai','cai.id=self.caiid')
                ->where($where)->where($where222)->where($whereGonger)->where($whereCai)->select()->toArray();
            foreach ($info as $item){
                $item['lai'] = '分包进度款';
                $item['table'] = 'subbao_shen';
                $list[] = $item;
            }
        }

        if(!isset($param['lai']) || empty($param['lai']) || $param['lai'] == 'device_jie'){
            $whereGonger = [];
            if(isset($param['gonger']) && !empty($param['gonger'])){
                $whereGonger[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
            }
            $whereCai = [];
            if(isset($param['cainame']) && !empty($param['cainame'])){
                $whereCai[] = ['cai.name','like', '%'.$param['cainame'].'%'];
            }
            $info = DeviceJieModel::alias('self')
                ->field('self.*,e.name as enginname,cai.name as caiid_name,cai.gonger')
                ->join('engin e','e.id=self.enginid')
                ->leftJoin('contract_cai cai','cai.id=self.caiid')
                ->where($where)->where($where222)->where($whereGonger)->where($whereCai)->select()->toArray();
            foreach ($info as $item){
                $item['lai'] = '租赁结算';
                $item['table'] = 'device_jie';
                $list[] = $item;
            }
        }

        if(!isset($param['lai']) || empty($param['lai']) || $param['lai'] == 'labour_ling'){
            $whereGonger = [];
            if(isset($param['gonger']) && !empty($param['gonger'])){
                $whereGonger[] = ['self.gonger','like', '%'.$param['gonger'].'%'];
            }
            $whereCai = [];
            if(isset($param['cainame']) && !empty($param['cainame'])){
                $whereCai[] = ['self.id','<', 0];
            }
            $info = LabourLingModel::alias('self')
                ->field('self.*,e.name as enginname')
                ->join('engin e','e.id=self.enginid')
                ->where($where)->where($whereGonger)->where($whereCai)->select()->toArray();
            foreach ($info as $item){
                $item['lai'] = '零星劳务';
                $item['table'] = 'labour_ling';
                $list[] = $item;
            }
        }

        if(!isset($param['lai']) || empty($param['lai']) || $param['lai'] == 'device_lin'){
            $whereGonger = [];
            if(isset($param['gonger']) && !empty($param['gonger'])){
                $whereGonger[] = ['self.gonger','like', '%'.$param['gonger'].'%'];
            }
            $whereCai = [];
            if(isset($param['cainame']) && !empty($param['cainame'])){
                $whereCai[] = ['self.id','<', 0];
            }
            $info = DeviceLinModel::alias('self')
                ->field('self.*,e.name as enginname')
                ->join('engin e','e.id=self.enginid')
                ->where($where)->where($whereGonger)->where($whereCai)->select()->toArray();
            foreach ($info as $item){
                $item['lai'] = '临时机械';
                $item['table'] = 'device_lin';
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }




}
